package com.guangzhi.weijianzhi.mainhome;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.activity.picActivity;
import com.guangzhi.weijianzhi.http.HttpRequestUtils;
import com.guangzhi.weijianzhi.http.MyAsyncHttpResponseHandler;
import com.guangzhi.weijianzhi.utils.CommonUtils;
import com.guangzhi.weijianzhi.utils.Misc;

public class CompanyInfoActivity extends BaseActivity implements OnClickListener{

	private TextView mComName,mIndustry,mCom_type,mCom_scale,mCom_address,mCom_add,mCom_about,mCom_name,mCom_number;
    private ArrayList<ImageView> images = new ArrayList<ImageView>();
	private String[] splits;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_com_info_layout);
		String comId = getIntent().getStringExtra("comId");
		initView();
		initDate(comId);
	}

	private void initDate(String comapnyId) {
		
		HttpRequestUtils.doHttpMianComInfo(comapnyId, new MyAsyncHttpResponseHandler(getSupportFragmentManager(),getResources()) {
			
		

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				// TODO Auto-generated method stub
				super.onSuccess(arg0, arg1, arg2);
				try {
					JSONObject jsonO = new JSONObject(new String(arg2));
					System.out.println(jsonO.toString());
					
					if(jsonO.optBoolean("status")){
						
						JSONArray optJSONArray = jsonO.optJSONArray("data");
						
						if(optJSONArray.length()!=0){
							
							JSONObject optJSONObject = optJSONArray.optJSONObject(0);
							mComName.setText("\t\t"+optJSONObject.optString("company_name"));
							mIndustry.setText("\t\t"+optJSONObject.optString("vocation"));
							mCom_type.setText("公司类型:\t\t"+optJSONObject.optString("type"));
							mCom_scale.setText("公司规模:\t\t"+optJSONObject.optString("scale"));
							mCom_address.setText("公司地址:\t\t"+optJSONObject.optString("area"));
							mCom_add.setText("\t\t"+optJSONObject.optString("full_address"));
							mCom_about.setText("\t\t"+optJSONObject.optString("intro"));
							mCom_name.setText("联系人姓名:\t\t"+optJSONObject.optString("contacts_name"));
							mCom_number.setText("联系方式:\t\t"+optJSONObject.optString("contacts_tel"));
							String picUrl = optJSONObject.optString("picurl");
							
							if(Misc.notNull(picUrl)){
								
								parserImage(picUrl);
							}
						}
						
						
					}else{
						Misc.alert(jsonO.optString("errMessage"));
						
						
					}
					
					
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			private void parserImage(String picUrl) {
				// TODO Auto-generated method stub
				
				splits = picUrl.split("\\|");
				
				for(int j=0;j<splits.length;j++){
					ImageView imageView = images.get(j);
					imageView.setOnClickListener(CompanyInfoActivity.this);
					imageView.setVisibility(View.VISIBLE);
					CommonUtils.showBgImage(splits[j], imageView);
				}
				
				
				
			}

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				super.onFailure(arg0, arg1, arg2, arg3);
			};
		});
		
	}

	private void initView() {
		
		initTitle();
		titleTextV.setText("公司详情");
		goBack.setOnClickListener(this);
		mComName = (TextView) findViewById(R.id.com_name);
		mIndustry = (TextView) findViewById(R.id.industry);
		 mCom_type = (TextView) findViewById(R.id.com_type);
		 mCom_scale = (TextView) findViewById(R.id.com_scale);
		 mCom_address = (TextView) findViewById(R.id.com_address);
		 mCom_add = (TextView) findViewById(R.id.address_add);
		 mCom_about = (TextView) findViewById(R.id.com_about);
		 mCom_name = (TextView) findViewById(R.id.contact_name);
		 mCom_number = (TextView) findViewById(R.id.contact_number);
		 images.clear();
		ImageView image_one = (ImageView) findViewById(R.id.image_one);
		ImageView image_two = (ImageView) findViewById(R.id.image_two);
		ImageView image_three = (ImageView) findViewById(R.id.image_three);
		images.add(image_one);
		images.add(image_two);
		images.add(image_three);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent ;
		switch (v.getId()) {
		
		case R.id.image_one:
			 intent = new Intent(this, picActivity.class);
			intent.putExtra("url", splits[0]);
			startActivity(intent);
			break;
		case R.id.image_two:
			 intent = new Intent(this, picActivity.class);
				intent.putExtra("url", splits[1]);
				startActivity(intent);
			break;
		case R.id.image_three:
			intent = new Intent(this, picActivity.class);
			intent.putExtra("url", splits[2]);
			startActivity(intent);
			break;

		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
}
